package com.fw.ui.view.profile.service.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.ErrorBean;
import com.fw.service.serviceprofile.bean.ServiceProfileBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.bao.ServiceProfileBAO;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.profile.DeleteCashServiceProfileView;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class DeleteCashServiceProfileDeleteButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private ServiceProfileBAO serviceProfileBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private UICommonUtils uiCommonUtils;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	DeleteCashServiceProfileView deleteServiceProfileView = (DeleteCashServiceProfileView) this.getBasedComponent();
	ServiceProfileBean serviceProfileBean = deleteServiceProfileView.getServiceProfileBean();
	ApplicationUI applicationUI = (ApplicationUI) deleteServiceProfileView.getUI();
	sessionInfoUtils.setSessionInfoDetails(serviceProfileBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	Map<String, Serializable> mapResult = this.serviceProfileBAO.deleteServiceProfile(serviceProfileBean);
	Map<String, Object> result = new HashMap<String, Object>();
	if (mapResult != null) {
	    List<ErrorBean> errorBeans = (List<ErrorBean>) mapResult.get(List.class.getName() + "." + ErrorBean.class.getName());
	    ServiceProfileBean resultServiceProfileBean = (ServiceProfileBean) mapResult.get(ServiceProfileBean.class.getName());
	    if (errorBeans != null && !errorBeans.isEmpty()) {
		result.put(MessageBAO.MESSAGE_WARNING_TYPE, errorBeans);
	    } else {
		if (resultServiceProfileBean != null && resultServiceProfileBean.getId() != null) {
		    deleteServiceProfileView.setServiceProfileBean(resultServiceProfileBean);
		    deleteServiceProfileView.getLblConfirmDeleteMessage().setValue("");
		    deleteServiceProfileView.getBtnCancel().setCaption(
			    this.messageBAO.getMessage("com.fw.ui.view.profile.DeleteCashServiceProfileView.button.close"));
		    this.setAutoEnabled(false);
		    uiCommonUtils.setEnableUIComponent(deleteServiceProfileView.getBtnDelete(), false);
		    result.put(MessageBAO.MESSAGE_INFO_TYPE, "com.fw.ui.view.profile.DeleteCashServiceProfileView.message.deleted");
		} else {
		    result.put(MessageBAO.MESSAGE_WARNING_TYPE,
			    "com.fw.ui.view.profile.DeleteCashServiceProfileView.message.deleted.failed");
		}
	    }
	} else {
	    result.put(MessageBAO.MESSAGE_WARNING_TYPE, "message.error.common");
	}
	return result;
    }

    @Override
    public boolean validate(ClickEvent event) {
	return true;
    }

}
